package com.ming.service.impl;

import com.lly835.bestpay.enums.BestPayTypeEnum;
import com.lly835.bestpay.model.PayRequest;
import com.lly835.bestpay.model.PayResponse;
import com.lly835.bestpay.model.RefundRequest;
import com.lly835.bestpay.model.RefundResponse;
import com.lly835.bestpay.service.impl.BestPayServiceImpl;
import com.ming.dto.OrderDTO;
import com.ming.enums.ResultEnum;
import com.ming.exception.SellException;
import com.ming.service.OrderService;
import com.ming.service.PayService;
import com.ming.utils.MathUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by LIANG on 2018/3/25.
 */
@Service
@Slf4j
public class PayServiceImpl implements PayService{
    @Autowired
    private BestPayServiceImpl payService;
    private static final String ORDER_NAME  = "微信点餐支付";

    @Autowired
    private OrderService orderService;
    @Override
    public PayResponse create(OrderDTO orderDTO) {
        PayRequest payRequest = new PayRequest();
        payRequest.setOrderId(orderDTO.getOrderId());
        payRequest.setOpenid(orderDTO.getBuyerOpenid());
        payRequest.setOrderAmount(orderDTO.getOrderAmount().doubleValue());
        payRequest.setOrderName(ORDER_NAME);
        payRequest.setPayTypeEnum(BestPayTypeEnum.WXPAY_H5);
        log.info("【微信支付】payRequest={}"+payRequest);
        PayResponse payResponse = payService.pay(payRequest);
        log.info("【微信支付】payResponse={}"+payResponse);
        return payResponse;
    }

    @Override
    public PayResponse asyncNotify(String notifyData) {
        //1。验证签名
        //2.支付的状态
        //3.支付的金额
        //4支付人
        PayResponse payResponse = payService.asyncNotify(notifyData);
        log.info("【微信异步通知】，payResponse={}",payResponse);
        OrderDTO orderDTO  = orderService.findOne(payResponse.getOrderId());
        //判断订单是否存在
        if (orderDTO ==null){
            log.error("【微信异步通知,订单不存在】，orderDTO={}",orderDTO);
            throw new SellException(ResultEnum.ORDER_DETAIL_EMPTY);
        }
        if (!MathUtil.equals(orderDTO.getOrderAmount().doubleValue(),payResponse.getOrderAmount().doubleValue())){
            log.error("【微信支付】，异步通知，订单金额不一致，微信通知金额{}，系统金额{}",
                    payResponse.getOrderId(),
                    payResponse.getOrderAmount(),
                    orderDTO.getOrderAmount());
            throw new SellException(ResultEnum.ORDER_DETAIL_EMPTY);
        }
        //修改订单支付状态
        orderService.paid(orderDTO);
        return payResponse;
    }

    @Override
    public RefundResponse refund(OrderDTO orderDTO) {
        RefundRequest refundRequest = new RefundRequest();
        refundRequest.setOrderId(orderDTO.getOrderId());
        refundRequest.setOrderAmount(orderDTO.getOrderAmount().doubleValue());
        refundRequest.setPayTypeEnum(BestPayTypeEnum.WXPAY_H5);
        log.info("【微信退款】request={}",refundRequest);
        RefundResponse refundResponse = payService.refund(refundRequest);
        log.info("【微信退款】refundResponse={}",refundResponse);
        return refundResponse;
    }
}
